package com.z.kq.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.z.common.context.UserContext;
import com.z.common.web.vo.request.PageVo;
import com.z.kq.entity.Leave;
import com.z.kq.mapper.LeaveMapper;
import com.z.kq.service.ILeaveService;
import com.z.sys.entity.Dept;
import com.z.sys.service.IDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假管理Service业务层处理
 *
 * @author z
 * @date 2023-03-06
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper,Leave> implements ILeaveService {

    @Resource
    private IDeptService deptService;

    /**
     * 查询请假管理
     * @param leave 请假管理
     * @param pageVo
     * @return 请假管理 分页集合
     * */
    @Override
    public PageInfo<Leave> selectLeavePage(Leave leave, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        List<Leave> data = selectLeaveList(leave);
        return new PageInfo<>(data);
    }
    @Override
    public  List<Leave> selectLeaveList(Leave leave) {
        if("1".equals(leave.getDeptId())){
            leave.setDeptId("");
        }
        if(StringUtils.isEmpty(leave.getDeptId())){
            int powerLevel = UserContext.getPowerLevel("考勤");
            if(powerLevel != 0){
                List<Dept> deptList = deptService.getDeptListByRole();
                String full_path_sql = "(";
                if(deptList != null && deptList.size() > 0){
                    for(Dept dept : deptList){
                        full_path_sql += ("d.full_path like '" + dept.getFullPath() + "/%' or d.full_path = '" + dept.getFullPath() + "' or ");
                    };
                }

                full_path_sql += " user_id = "+UserContext.currentUser().getId()+")";
                Map<String, Object> map = new HashMap<>();
                map.put("full_path_sql", full_path_sql);
                leave.setParams(map);

            }
        }
        return baseMapper.selectLeaveList(leave);
    }

}
